package com.skyolder.milkbee.web.rest.pub;

import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.codahale.metrics.annotation.Timed;
import com.skyolder.milkbee.domain.Advertisement;
import com.skyolder.milkbee.service.AdvertisementService;
import com.skyolder.milkbee.web.rest.util.PaginationUtil;

import io.swagger.annotations.ApiParam;

/**
 * REST controller for managing Advertisement.
 */
@RestController("pubAdvertisementResource")
@RequestMapping("/api/pub")
public class AdvertisementResource {

    private final Logger log = LoggerFactory.getLogger(AdvertisementResource.class);
        
    @Inject
    private AdvertisementService advertisementService;



    /**
     * GET  /advertisements : get all the advertisements.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of advertisements in body
     * @throws URISyntaxException if there is an error to generate the pagination HTTP headers
     */
    @GetMapping("/advertisements/category/{categoryId}")
    @Timed
    public ResponseEntity<List<Advertisement>> getAllAdvertisements(@PathVariable Long categoryId,@ApiParam Pageable pageable)
        throws URISyntaxException {
        log.debug("REST request to get a page of Advertisements");
        Page<Advertisement> page = advertisementService.queryByCategory(categoryId,pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/advertisements");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /advertisements/:id : get the "id" advertisement.
     *
     * @param id the id of the advertisement to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the advertisement, or with status 404 (Not Found)
     */
    @GetMapping("/advertisements/{id}")
    @Timed
    public ResponseEntity<Advertisement> getAdvertisement(@PathVariable Long id) {
        log.debug("REST request to get Advertisement : {}", id);
        Advertisement advertisement = advertisementService.findOne(id);
        return Optional.ofNullable(advertisement)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }
}
